<template>
  <div class="oldSales" style="height: 100%">
    <el-row style="height: 100%" v-if="!button || (button.event!='add()' && button.event!='edit()' && button.event!='detail()') ">
      <el-col :span="4" style="height: 100%;border-right: 1px solid #eee">
        <CommonTree label-name="销售组织架构" :tree-props="treeProps" :data="treeData" @treeChange="treeChange" :close="true"></CommonTree>
      </el-col>
      <el-col :span="20">
        <CommonBtn  label-name="销售组织架构管理" :buttons="buttons" @onButtonClick="onButtonClick">
          <span slot="appendCondition" style="color:red;font-size:14px;font-weight: bold">{{buName}}</span>
        </CommonBtn>
        <CommonList ref="list" :columns="columns" :list-url="listUrl" @tableSelect="tableSelect"></CommonList>
        <CommonInport :out-btn="button" @close="close" :inportUrl="$api.API_OLDSALEFRAMEWORK_LIST_IMPORT" :template-url="templateUrl"></CommonInport>
        <CommonExport :out-btn="button" :props="exportColumns"
                      @close="close" :export-url="$api.API_SALEFRAMEWORK_LIST_EXPORT"></CommonExport>
      </el-col>
    </el-row>

    <CommonEdit v-if="button && (button.event=='detail()' || button.event=='edit()') && (buIndex == 0 || buIndex == 1 || buIndex==2)"
                :outBtn="button" :props="add"
                :original-data="row" :edit-url="updateUrl"
                @editBack="editBack"></CommonEdit>

    <RelationAreaEdit  v-if="button && (button.event=='add()' || button.event=='edit()' || button.event=='detail()') && buIndex == 3"
                       :outBtn="button"  @editBack="close" :b-uid="bUid" :original-data="row"> </RelationAreaEdit>

    <EnsurePopUp :out-btn="button" @close="close" @refresh="refresh" :original-data="row"
                ></EnsurePopUp>

  </div>
</template>

<script>
    import CommonBtn from "../../common/CommonBtn";
    import CommonTree from "../../common/CommonTree";
    import api from '../../../api/api'
    import CommonList from "../../common/CommonList";
    import CommonInport from "../../common/CommonInport";
    import CommonEdit from "../../common/CommonEdit";
    import RelationAreaEdit from "../../common/RelationAreaEdit";
    import EnsurePopUp from "../../common/EnsurePopUp";
    import CommonExport from "../../common/CommonExport";

    const columns1=[{label:'业务单位名称',prop:'buCode'},{label:'区域名称',prop:'buName'},{label:'是否有效',prop:'isValid',customSet(value) {if (value == 1) {return 'true'}return 'false'}},{label:'备注',prop:'memo'},{label:'创建者',prop:'createUserName'},{label:'最后修改者',prop:'modifyUserName'},{label:'创建时间',prop:'createTime'},{label:'修改时间',prop:'modifyTime'}]
    const columns2=[{label:'业务单位名称',prop:'zoneCode'},{label:'区域名称',prop:'zoneName'},{label:'是否有效',prop:'isValid',customSet(value) {if (value == 1) {return 'true'}return 'false'}},{label:'备注',prop:'memo'},{label:'创建者',prop:'createUserName'},{label:'最后修改者',prop:'modifyUserName'},{label:'创建时间',prop:'createTime'},{label:'修改时间',prop:'modifyTime'}]
    const columns3=[{label:'区域名称',prop:'zoneName1'},{label:'Area名称',prop:'regionName'},{label:'是否有效',prop:'isValid',customSet(value) {if (value == 1) {return 'true'}return 'false'}},{label:'备注',prop:'memo'},{label:'创建者',prop:'createUserName'},{label:'最后修改者',prop:'modifyUserName'},{label:'创建时间',prop:'createTime'},{label:'修改时间',prop:'modifyTime'}]
    const columns4=[{label:'省份名称',prop:'provinceName'},{label:'城市名称',prop:'cityName'},{label:'是否有效',prop:'isValid',customSet(value) {if (value == 1) {return 'true'}return 'false'}},{label:'备注',prop:'memo'},{label:'创建者',prop:'createUserName'},{label:'最后修改者',prop:'modifyUserName'},{label:'创建时间',prop:'createTime'},{label:'修改时间',prop:'modifyTime'}]
    const columns  =[columns1,columns2,columns3,columns4]
    const url = api.API_SALES_ORGANIZTION_OldFRAMEWORK_LIST
    const labels = ['查看BU信息','可新增编辑区域信息','可新增编辑Area信息','可新增编辑城市信息']

    const  add1 = {
        label:'业务单位',
        addItems:[
            {name:'业务单位编码',prop:'buCode',type:'text',required:true,disable:true,placeholder:'系统自动生成'},
            {name:'业务单位名称',prop:'buName',type:'text',required:true},
            {name:'排序',prop:'sortCode',type:'text',required:true,disable:true,placeholder: '系统自动生成'},
            {name:'备注',prop:'memo',type:'textarea',placeholder:'最多100个中文'},
        ],
    }
    const  add2 = {
        label:'大区信息',
        addItems:[
            {name:'区域代码',prop:'zoneCode',type:'text',required:true,disable:true,placeholder:'系统自动生成'},
            {name:'区域名称',prop:'zoneName',type:'text',required:true},
            {name:'业务单位',prop:'fkBu',type:'select',required:true,optionProxy:'buName',optionValue:'pkid'},
            {name:'排序',prop:'sortCode',type:'text',required:true,disable:true,placeholder: '系统自动生成'},
            {name:'备注',prop:'memo',type:'textarea'}
        ],
    }
    const  add3 = {
        label:'区域信息',
        addItems:[
            {name:'Area代码',prop:'regionCode',type:'text',required:true,disable:true,placeholder:'系统自动生成'},
            {name:'Area名称',prop:'regionName',type:'text',required:true},
            {name:'排序',prop:'sortCode',type:'text',disable:true,placeholder: '系统自动生成'},
            {name:'归属区域',prop:'fkZone',type:'select',required:true,optionProxy:'',optionAppendProxy:'zoneName',optionValue:'pkid'},
            {name:'备注',prop:'memo',type:'textarea'},
            {name:'重命名',prop:'rename',type:'text'}
        ],
    }
    const add4 = {
    }

    const adds=[add1,add2,add3,add4]

    const updateUrls= [api.API_BUSETTING_UPDATE_OLDBU,api.API_UPDATEOLDZONE,api.API_UPDATEOLDREGION]


  export default {
      data(){
          return{
              treeProps: {
                  children: 'children',
                  label: 'BuName'
              },
              treeData: [],
              buttons: [
                  this.$config.BTNS.EXPORT,
                  this.$config.BTNS.DATAINPORT,
                  this.$config.BTNS.EDIT,
              ],
              columns: columns[0],
              button: null,
              listUrl:url,
              row:null,
              add:add1,
              buIndex:0,
              buName:labels[0],
              bUid:'',
              options:[],
              guid:'',
              updateUrl:'',
              templateUrl:'/static/template/组织架构导入模板.xls',
              exportColumns:[{label:'年份'},{label:'Bu'},{label:'Region'},{label:'Area'},{label:'省份'},{label:'城市'},{label:'是否有效'},{label:'创建时间'},{label:'最后修改时间'}]
          }
      },
      methods:{
          onButtonClick(item) {
              if (item.event == 'edit()' || item.event == 'detail()') {
                  if (!this.row || JSON.stringify(this.row) == '{}') {
                      this.$message.error("请选择一行数据")
                      return
                  }
              }
              if (item.event == 'edit()') {
                  if (this.buIndex == 0 ||this.buIndex == 1 || this.buIndex == 2) {
                      this.updateUrl = updateUrls[this.buIndex]
                  }
                  this.getOptions()
              }
              this.button = item
          },
          initData() {
              this.$http.get(this.$api.API_OLD_SALES_ORGANIZTION_FRAMEWOrK).then(resp=>{
                  this.treeData = resp
              },msg=>{})
          },
          treeChange(item) {
              this.row=null
              this.listUrl = url+'?typeid='+item.DataType+'&guid='+item.ID
              this.columns = columns[item.DataType]
              this.add = adds[item.DataType]
              this.buIndex = item.DataType
              this.guid = item.ID
              this.$nextTick(()=>{
                  this.$refs.list.search()
              })
              this.buName = labels[this.buIndex]

              if (this.buIndex == 3) {
                  let pid = item.ParentID
                  for (let i=0;i<this.treeData.length;i++) {
                      for (let j=0;j<this.treeData[i].children.length;j++) {
                          if (this.treeData[i].children[j].ID == pid) {
                              this.bUid = this.treeData[i].ID
                              return
                          }
                      }
                  }
              }
          },
          tableSelect(item) {
              this.row = item
          },
          openItem(item) {
              this.button = this.$config.BTNS.DETAIL
          },
          close(item){
              this.button = item
          },
          editBack() {
              this.close()
              this.$nextTick(()=>{
                  this.$refs.list.search()
                  this.initData()
              })
          },
          refresh() {
              this.$refs.list.search()
          },
          getOptions() {
              let url = ''
              if (this.buIndex == 1) {
                  url = this.$api.API_SALES_ORGANIZTION_BULIST
              } else if (this.buIndex == 2) {
                  url = this.$api.API_QUERYZONE
              } else if (this.buIndex == 3 || this.buIndex == 0) {
                  return
              }
              this.$http.get(url,{guid:this.guid}).then(resp=>{
                  this.options = resp.list
                  for (let i=0;i<this.options.length;i++) {
                      if (this.buIndex == 1) {
                          this.options[i].label = this.options[i].buName
                      } else if (this.buIndex == 2) {
                          this.options[i].label = this.options[i].buName+'【'+this.options[i].zoneName+'】'
                      }
                  }
                  this.$nextTick(()=>{
                      if (this.buIndex == 1) {
                          this.$set(this.add.addItems[2],'options',this.options)
                      } else if (this.buIndex == 2) {
                          this.$set(this.add.addItems[3],'options',this.options)
                      }
                  })
              })
          }
      },
      created() {
          this.initData()
      },
      components:{RelationAreaEdit, CommonInport,CommonList, CommonTree, CommonBtn, CommonEdit, EnsurePopUp, CommonExport}
  }
</script>
